package com.food.eat.search.controller;

import com.food.eat.search.service.ItemElasticSearchService;
import com.food.eat.util.FoodResultJson;
import com.food.eat.util.PagedGridResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author:RenPu
 * @Date: 2020/8/11 18:21
 * @Version: 1.0
 * @description:
 */

@RestController
@RequestMapping("/items")
public class ItemsElasticSearchController {


    @Resource
    private ItemElasticSearchService itemElasticSearchService;


    /**
     * PathVariable：注解主要
     * RequestParam
     */

    @ApiOperation(httpMethod = "GET", value = "首页商品查询接口实现", notes = "首页商品查询接口实现")
    @GetMapping("/es/search")
    public FoodResultJson search(
            String keywords,
            Integer page,
            Integer pageSize,
            String sort) {
        if (StringUtils.isBlank(keywords)) {
            return FoodResultJson.errMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 20;
        }
        //es分頁默認值為1
        page--;


        PagedGridResult pagedGridResult = itemElasticSearchService.searchItemsMessageInfo(keywords, sort, page, pageSize);

        return FoodResultJson.ok(pagedGridResult);
    }



    @ApiOperation(httpMethod = "GET", value = "首页商品查询接口实现", notes = "首页商品查询接口实现")
    @GetMapping("/es/suggestWord")
    public FoodResultJson suggestWord(){

        return FoodResultJson.ok();
    }

}
